﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BruteForceSerializer.Normalization;

namespace XcoAppSpaces.Serialization.Json
{
	class SerializableCheck
	{
		public static bool CanTypeBeSerialized(Type type, out string errorInfo)
		{
			errorInfo = null;
			
			//type needs to be public
			if (!type.IsPublic && !type.IsNestedPublic)
			{
				errorInfo = "The type " + type.FullName + " is not public.";
				return false;
			}
			
			//types that belong to the simple types (string, int, DateTime, ...), or 
			//such that are not classes (e.g. interfaces, structs) cannot be checked further
			if (type.IsSimpleType() || !type.IsClass)
			{
				return true;
			}
			
			//type needs a parameterless constructor
			if (type.GetConstructor(new Type[]{}) == null)
			{
				errorInfo = "The type " + type.FullName + " doesn't have an empty constructor";
				return false;
			}
			
			return true;
		}
	}
}
